package org.practice.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.practice.form.CartItemForm;
import org.practice.model.User;
import org.practice.service.CartService;
import org.practice.service.bo.CartBO;
import org.practice.service.impl.CartServiceImpl;
import org.practice.util.Constant;
import org.practice.util.CookieUtil;

public class CartAction extends CartMappingDispatchAction {
	CartService service=new CartServiceImpl();
	
	public ActionForward show(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CartBO cart=getCart(request);
		service.countEffectiveCartItems(cart);
		service.countDropCartItems(cart);
		service.calculateTotalPrice(cart);
		service.calculateDangPrice(cart);
		if (service.isEmpty(cart)) {
			ActionMessages errors = new ActionMessages();
			errors.add("error.cart.empty",
					new ActionMessage("error.cart.empty"));
			saveErrors(request, errors);

		}
		return mapping.findForward("success");
	}

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CartBO cart=getCart(request);
		CartItemForm f = (CartItemForm) form;
		service.addCartItem(cart,f.getProductId());
		saveToCookie(response, cart);
		return mapping.findForward("success");
	}

	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CartBO cart=getCart(request);
		service.calculateTotalPrice(cart);
		service.calculateDangPrice(cart);
		CartItemForm f = (CartItemForm) form;
		service.updateNum(cart,f.getProductId(), f.getNum());
		saveToCookie(response, cart);
		return mapping.findForward("success");
	}

	public ActionForward drop(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CartBO cart=getCart(request);
		CartItemForm f = (CartItemForm) form;
		service.drop(cart,f.getProductId());
		saveToCookie(response, cart);
		return mapping.findForward("success");
	}

	public ActionForward recovery(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CartBO cart=getCart(request);
		CartItemForm f = (CartItemForm) form;
		service.recovery(cart,f.getProductId());
		saveToCookie(response, cart);
		return mapping.findForward("success");
	}

	private CartBO getCart(HttpServletRequest request) throws Exception {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute(Constant.SESSION_KEY_USER);
		CartBO cart = (CartBO) session.getAttribute(Constant.SESSION_KEY_CART);
		if (cart == null) {
			cart = new CartBO(user.getId());
			String cartCookieVal = CookieUtil.findCookie(request,Constant.COOKIE_KEY_CARTID);
			if (cartCookieVal != null) {
				service.load(cart,cartCookieVal);
			}
			session.setAttribute(Constant.SESSION_KEY_CART, cart);
		}
		return cart;
	}

	private void saveToCookie(HttpServletResponse response, CartBO cart)
			throws Exception {
		CookieUtil.addCookie(response, Constant.COOKIE_KEY_CARTID, service.generateSerialize(cart));
	}
}
